package com.yz.platform.modules.biz.service.impl;

import com.google.common.base.Strings;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yz.platform.common.utils.MD5Util;
import com.yz.platform.common.utils.UUIDHelper;
import com.yz.platform.modules.biz.dao.ActivationCodeDao;
import com.yz.platform.modules.biz.dao.AppDao;
import com.yz.platform.modules.biz.dao.ScriptDao;
import com.yz.platform.modules.biz.entity.ActivationCodeEntity;
import com.yz.platform.modules.biz.entity.AppEntity;
import com.yz.platform.modules.biz.entity.ScriptEntity;
import com.yz.platform.modules.biz.service.ScriptService;
import com.yz.platform.modules.sys.dao.SysConfigDao;
import com.yz.platform.modules.sys.entity.SysConfigEntity;
import com.yz.platform.modules.sys.service.SysConfigService;


@Transactional(readOnly = true,rollbackFor = Exception.class)
@Service("scriptService")
public class ScriptServiceImpl implements ScriptService {

    private Logger log = LoggerFactory.getLogger(ScriptServiceImpl.class);


    @Autowired
    private SysConfigDao sysConfigDao;

    @Autowired
    private ActivationCodeDao activationCodeDao;

    @Autowired
    private ScriptDao scriptDao;

    @Autowired
    private AppDao appDao;

    @Override
    public ScriptEntity queryObject(Integer scriptId) {
        return scriptDao.queryObject(scriptId);
    }

    @Override
    public ScriptEntity getByScriptNo(String scriptNo) {
        return scriptDao.getByScriptNo(scriptNo);
    }

    @Override
    public List<ScriptEntity> queryList(Map<String, Object> map) {
        return scriptDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return scriptDao.queryTotal(map);
    }


    /**
     * 保存脚本同时生成对应的激活码
     */
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    @Override
    public void save(ScriptEntity script) {


        script.setScriptNo(UUIDHelper.getId());

        JSONObject scriptContent = new JSONObject();

        try {
            if (!Strings.isNullOrEmpty(script.getStoreUrl())) {
                scriptContent.put("storeUrl", script.getStoreUrl());
            }

            if (!Strings.isNullOrEmpty(script.getStorePwd())) {
                scriptContent.put("storePwd", script.getStorePwd());
            }
        } catch (JSONException e) {
            log.error("build scriptContent fail", e);
        }

        script.setScriptContent(scriptContent.toString());
        script.setCreateTime(new Date());
        script.setUpdateTime(new Date());

        AppEntity appEntity = appDao.queryObject(script.getAppId());
        if (appEntity != null){
            script.setAccessToken(MD5Util.MD5(script.getScriptNo()+appEntity.getPackageName()));
        }


        scriptDao.save(script);


    }

    /**
     * 默认当前时间一个月
     */
    private Date calExpires() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        return Calendar.getInstance().getTime();
    }

    @Transactional(readOnly = false,rollbackFor = Exception.class)
    @Override
    public void update(ScriptEntity script) {
        scriptDao.update(script);
    }

    @Transactional(readOnly = false,rollbackFor = Exception.class)
    @Override
    public void delete(Integer scriptId) {
        scriptDao.delete(scriptId);
    }

    @Transactional(readOnly = false,rollbackFor = Exception.class)
    @Override
    public void deleteBatch(Integer[] scriptIds) {
        scriptDao.deleteBatch(scriptIds);
    }

}
